package source.events.action;

import source.model.*;
import source.model.type.*;

public class ExecuteMission {
	private int playerId;
	private UnitMission mission;

	public ExecuteMission( int pid, UnitMission um ) {
		playerId = pid;
		mission = um;
	}

	public ActionResult execute( DataHandler dh ) {
		dh.setCurrentSelection( mission.getUnitID() );

		Unit u = (Unit)dh.objectQuery( mission.getUnitID() );

		Action nextAction = mission.getNextAction();
		ActionResult res;
		while ( nextAction != null ) {
			res = nextAction.execute(dh);
			switch (res) {
				case FAILED:
					return ActionResult.SUCCESS;
				case IMPOSSIBLE:
					mission.invalidate();
					u.setMissionFlag(false);
					return ActionResult.SUCCESS;
				case SUCCESS:
					mission.removeAction();
			}
			nextAction = mission.getNextAction();
		}
		mission.invalidate();
		u.setMissionFlag(false);
		return ActionResult.SUCCESS;
	}
}
